package tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class ConsolePrintStream extends PrintStream {
	private PrintStream oldPrintStream;
	private SiftedQueue infoQueue;
	private ArrayList<String> infos;
	public boolean isLog=true;
	public ConsolePrintStream(File file) throws FileNotFoundException  {
		this(file, null);
	}
	public ConsolePrintStream(File file, PrintStream out) throws FileNotFoundException {
		super(file);
		oldPrintStream=out;
		infoQueue=new SiftedQueue(200);
		infos=new ArrayList<String>();
	}
	@Override
	public void println(String text) {
		if(isLog) {
			Date date = new Date();
			SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
			text=formatter.format(date)+":\r\n"+text;
			if(oldPrintStream!=null) {
				oldPrintStream.println(text);
			}
			super.println(text);
			if(text.indexOf("/@ServerConsole")<0) {
				infoQueue.push(text);
			}
		}
	}
	
	public ArrayList<String> getInfos(int maxCount) {
		infos.clear();
		for(int i=0;i<maxCount;i++) {
			String text=infoQueue.pop();
			if(text==null) {
				break;
			}else {
				infos.add(text);
			}
		}
		return infos;
	}
	public void pushInfos(String[] infos) {
		for(int i=0;i<infos.length;i++) {
			infoQueue.push(infos[i]);
		}
	}
}
